// *********************************************************
//      Copyright (c) Kosta Gaitanis
//      All rights reserved
//
//      k.gaitanis@gmail.com
//
// *********************************************************

#ifndef GLWIDGET_H
#define GLWIDGET_H

#include <QGLWidget>
#include <QTime>

#include "GLPipeline.h"

class FrameCounter;
class QGLShaderProgram;
class GLWidget : public QGLWidget
{
    Q_OBJECT
public:
    GLWidget(QWidget *parent = 0);
    ~GLWidget();

public slots:
    void setPipelineParameter(const QString& name, const QVariant& value);

signals:
    void requestInitializeGL();
    void requestPaint();
    void GLresized(int width, int height);

    void moveCameraBy(int x, int y);
    void rotateCameraBy(double angle);
    void zoomBy(int zoomFactor);
    void sceneRenderTime(double msecs);
    void frameRate(double frameRate);
    void playPause();

protected:
    virtual void initializeGL();
    virtual void paintGL();
    virtual void resizeGL(int width, int height);
    virtual void mouseMoveEvent(QMouseEvent *event);
    virtual void mousePressEvent(QMouseEvent *event);
    virtual void wheelEvent(QWheelEvent *event);
    virtual void keyPressEvent(QKeyEvent *event);

private:
    GLPipeline *m_pipeline;

    QPoint m_previousPos;
    FrameCounter* m_sceneFrameCounter;
};

class FrameCounter : public QObject
{
    Q_OBJECT
public:
    FrameCounter(int samples = 10, QObject* parent = 0);
    double getFrameRate() const;
    double getRenderingTime() const;

signals:
    void frameRate(double framerate);
    void renderingTime(double msecs);

public slots:
    void onNewFrameStart();
    void onNewFrameEnd();

private:
    int m_samples;
    QTime m_renderTime;
    QTime m_frameRateTime;
    double m_frameRate;
    double m_renderingTime;

    QList<double> m_renderTimesList;


};

#endif // GLWIDGET_H
